package com.example.market.controller;

import com.example.market.entity.Employee;
import com.example.market.entity.Goods;
import com.example.market.repository.GoodsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/goods")
public class GoodsHandler {
   @Autowired
    private GoodsRepository goodsRepository;

    @GetMapping("/findAll/{page}/{size}")
    public Page<Goods> findAll(@PathVariable("page") Integer page, @PathVariable("size") Integer size){
        Pageable pageable = PageRequest.of(page-1,size);
        return goodsRepository.findAll(pageable);
    }

    @PostMapping("/save")
    public String save(@RequestBody Goods goods){
        Goods result = goodsRepository.save(goods);
        if(result != null){
            return "success";
        }else{
            return "error";
        }
    }

    @GetMapping("/findById/{id}")
    public Goods findById(@PathVariable("id") Integer gno){
        return goodsRepository.findById(gno).get();
    }

    @PutMapping("/update")
    public String update(@RequestBody Goods goods ){
        Goods result = goodsRepository.save(goods);
        if(result != null){
            return "success";
        }else{
            return "error";
        }
    }

    @DeleteMapping("deleteById/{id}")
    public void deleteById(@PathVariable("id") Integer gno){
        goodsRepository.deleteById(gno);

    }
}
